package config

import (
	"common-api/logs"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"log"
	"os"
)

var AppConf = InitAppConfig()

type Config struct {
	ServerConfig *TargetServerConfig
}

type TargetServerConfig struct {
	Addr string
}

func InitAppConfig() *Config {
	v := viper.New()
	workDir, _ := os.Getwd()
	v.SetConfigName("app")
	v.SetConfigType("yml")
	v.AddConfigPath(workDir + "/config")
	err := v.ReadInConfig()
	if err != nil {
		log.Fatalln(err)
	}
	InitLog(v)

	return &Config{
		ServerConfig: InitServerConfig(v),
	}
}

func InitLog(viper *viper.Viper) {
	//从配置中读取日志配置，初始化日志
	log := &logs.LoggerConfig{
		FileName: viper.GetString("log.fileName"),
		MaxSize:  viper.GetInt("log.maxSize"),
		MaxAge:   viper.GetInt("log.maxAge"),
		Level:    logrus.Level(viper.GetUint32("log.level")),
	}
	logs.Init(log)
}

func InitServerConfig(viper *viper.Viper) *TargetServerConfig {
	return &TargetServerConfig{
		Addr: viper.GetString("target.addr"),
	}
}
